******************************************************
******** This file creates the national supply curves  *********
******* and saves them as *.csv files *****************
**** input file:  Plant_Level_Runs_070110.dta  (old file from Nathan)
**** input files: results_baseline_links_cleaned.dta", clear   (new file from Nathan)
**** input file:  run.dta  (relates "runs" to "prices")

clear matrix
drop _all
set mem 2g

***** user toggles:  sph is 1;  jeh is 2; ck is 3
global user=3    
****  write toggles:  $write_toggle==1 (writes supply curves) $write_toggle==0 (doesn't overwrite)
global write_toggle=0

***sph's directories
if $user==1{
global dir1="C:\AllStephen\My Dropbox\Jon n Chris\1 - Supply Curves\"
global dir2="C:\AllStephen\My Dropbox\Jon n Chris\1 - Supply Curves\Individual Fuel csv\"
global dir3="C:\AllStephen\My Dropbox\Jon n Chris\Write-up\Figures\"
}

***jeh's directories
if $user==2 {
global dir1="/Users/Jon/Dropbox/LandUse/1 - Supply Curves/"
global dir2="/Users/Jon/Dropbox/LandUse/1 - Supply Curves/Individual Fuel csv/"
global dir3="/Users/Jon/Dropbox/LandUse/Write-up/Figures/"
}
if $user==3 {
global dir1="/Users/knittel/Dropbox/Landuse/1 - Supply Curves/"
global dir2="/Users/knittel/Dropbox/Landuse/1 - Supply Curves/Individual Fuel csv/"
global dir3="/Users/knittel/Dropbox/Landuse/Write-up/Figures/"
}


cd "$dir1"
*
*** Calculate "factor" for type == "corngrain" 
***  New file from Nathan, doesn't have type =="wet_mill" |type=="dry_mill"
*use "results_baseline_links_cleaned.dta", clear
use "Plant_Level_Runs_070110.dta", clear
keep if type =="wet_mill" |type=="dry_mill"
keep run source_id type quant_tons 
duplicates drop run source_id type , force
rename source_id  dest_id
rename type dest_type
g factor=0.0893*0.657*(dest_type=="wet_mill")+0.1*0.657*(dest_type=="dry_mill")
*** if dry & wet mills at destination, factor is average factor
collapse (mean) factor [w=quant_tons], by (run dest_id)
g type="corngrain"
save temp1.dta, replace
*
***  New file from Nathan, doesn't replicate agg supply curves
use "results_baseline_links_cleaned.dta", clear  
*use "Plant_Level_Runs_070110.dta", clear
g ethtype= "corn" if type == "corngrain" 
replace ethtype="agres" if type == "ag_res" 
replace ethtype="orch" if type == "ovw"
replace ethtype="forest" if type == "forest" || type == "pulpwood"
replace ethtype="msw" if type == "msw_dirty" || type == "msw_paper" ||type == "msw_wood" ||type == "msw_yard" 
replace ethtype="msw_food" if type == "msw_food"
replace ethtype="hec" if type == "hec"
keep if ethtype !=""
*
merge m:1 run dest_id type  using temp1.dta
drop if _merge==2
drop _merge
*
**** these factors from Results_new_JH.xlsx  
***  "msw_dirty" has factor=0 since cannot be used for ethanol
***   "corngrain" factors computed above
*g factor = 0.657 if type == "corngrain" 
replace factor=0.0517059 if type == "ag_res" 
replace factor=0.0559764 if type == "ovw"
replace factor=0.0592614 if type == "forest" || type == "pulpwood"
replace factor=0 if type == "msw_dirty" 
replace factor= 0.0565020 if type == "msw_paper" 
replace factor= 0.0559764 if type == "msw_wood" 
replace factor= 0.0459900 if type == "msw_yard" 
replace factor= 0.0509175 if type == "msw_food"
replace factor=0.0508518 if type == "hec"
replace factor=factor*1000
g ethanol=factor*quant_tons
* 
*
****the next command generates our supply curves in Results_new_JH.xlsx
collapse (sum) ethanol, by (run ethtype)

****Create intercept for corn ethanol, below $1.20/gge corn ethanol is not economic
****run1 will correspond to MC of $1.10
set obs `=_N + 1'
replace run = "run1" in `=_N '
replace ethtype = "corn" in `=_N '
replace ethanol = 0 in `=_N '

fillin run ethtype
replace ethanol=0 if _fillin==1
drop _fillin
merge m:1 run using run.dta, nogen
***Add in price for run1
replace price = 1.10 if run == "run1"
sort ethtype price
move price ethanol

***************************
** Create .csv files for matlab code
***************************

cd "$dir2"
format ethanol %12.0g
outsheet price ethanol using agres.csv if ethtype =="agres", comma replace
outsheet price ethanol using corn.csv if ethtype =="corn", comma replace
outsheet price ethanol using forest.csv if ethtype =="forest", comma replace
outsheet price ethanol using hec.csv if ethtype =="hec", comma replace
outsheet price ethanol using msw.csv if ethtype =="msw", comma replace
outsheet price ethanol using msw_food.csv if ethtype =="msw_food", comma replace
outsheet price ethanol using orch_vin.csv if ethtype =="orch", comma replace

** OLD VERSION
*********************************
**  To create supply curves copy from the following and paste into .csv files
*********************************
*br price ethanol if ethtype =="agres"
*br price ethanol if ethtype =="corn"
*br price ethanol if ethtype =="forest"
*br price ethanol if ethtype =="hec"
*br price ethanol if ethtype =="msw"
*br price ethanol if ethtype =="msw_food"
*br price ethanol if ethtype =="orch"


******* Appendix Figure 1: Graph of supply curves *******
replace ethanol =ethanol /10^9
*** Convert prices to retail prices
replace price = price + 0.63
drop if ethtype =="corn" & price >3.63
#delimit ;
twoway (connected price ethanol if ethtype=="agres", msize(zero)) 
(connected price ethanol if ethtype=="corn", msize(zero) lpattern(dash))
(connected price ethanol if ethtype=="forest", msize(zero) lpattern(dash_dot))
(connected  price ethanol if ethtype=="hec", msize(zero) lpattern(longdash_dot))
(connected price ethanol if ethtype=="msw", msize(zero) lpattern(shortdash))
(connected price ethanol if ethtype=="msw_food", msize(zero) lpattern(shortdash_dot))
(connected price ethanol if ethtype=="orch", msize(zero) lpattern(longdash)), 
legend(order(1 "AgRes" 2 "Corn" 3 "Forest" 4 "HEC" 5 "MSW" 6 "MSW food" 7 "Orch" )rows(2)) ytitle(Retail Price ($/gge)) 
xtitle(Ethanol (billion gge))  
ylabel(1(1)6) 
graphregion(fcolor(white) color(white) icolor(white)) plotregion(fcolor(white) color(white) icolor(white));
graph export "$dir3/SupplyCurves.pdf", replace;

#delimit ;
twoway (connected price ethanol if ethtype=="agres", msize(zero)) 
(connected price ethanol if ethtype=="corn", msize(zero) lpattern(dash) lc(red))
(connected price ethanol if ethtype=="forest", msize(zero) lpattern(dash_dot) lc(orange))
(connected  price ethanol if ethtype=="hec", msize(zero) lpattern(longdash_dot) lc(purple))
(connected price ethanol if ethtype=="msw", msize(zero) lpattern(shortdash) lc(green))
(connected price ethanol if ethtype=="msw_food", msize(zero) lpattern(shortdash_dot) lc(gray))
(connected price ethanol if ethtype=="orch", msize(zero) lpattern(longdash) lc(blue)), 
legend(order(1 "AgRes" 2 "Corn" 3 "Forest" 4 "HEC" 5 "MSW" 6 "MSW food" 7 "Orch" )rows(2)) ytitle(Retail Price ($/gge)) 
xtitle(Ethanol (billion gge))  
ylabel(1(1)6) 
graphregion(fcolor(white) color(white) icolor(white)) plotregion(fcolor(white) color(white) icolor(white));

/* END */

